//
//  ViewController.swift
//  SwiftStudy
//
//  Created by wangtian on 15/7/14.
//  Copyright (c) 2015年 wangtian. All rights reserved.
//

import UIKit

class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var dataSource = [AnyObject]()
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.title = "主页"
        self.configueDataSource()
        self.setUpView()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //初始化view
    func setUpView()
    {
        UIViewController.setBackItemColorWithNaviVC(self.navigationController!, itemColor: UIColor.red)
         self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "mainCell")
    }
    
    //配置数据源
    func configueDataSource()
    {
        self.addAModel("PassValue", vcTitle: "类之间传值测试")
        self.addAModel("GeneralVC", vcTitle: "基础语法学习")
        self.addAModel("ErrorHanding", vcTitle: "异常处理")
    }
    
    func addAModel(_ vcClassName:String, vcTitle:String){
    
        let model:MainVCModel = MainVCModel()
        model.vcClassName = vcClassName
        model.vcClassTitle = vcTitle
        self.dataSource.append(model)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell:UITableViewCell = (tableView.dequeueReusableCell(withIdentifier: "mainCell") as UITableViewCell!)
        let model = self.dataSource[(indexPath as NSIndexPath).row] as! MainVCModel
        
        cell.textLabel!.text = model.vcClassTitle!

        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        let model = self.dataSource[(indexPath as NSIndexPath).row] as! MainVCModel
        let passValueVC:UIViewController = self.storyboard!.instantiateViewController(withIdentifier: model.vcClassName!) as UIViewController!
        self.setBackItemWithTitle("")
        self.navigationController!.pushViewController(passValueVC, animated: true)
    }
}

